package com.tian.print.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.tian.print.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class BaseActivity extends AppCompatActivity {

    /**
     * 顶部栏
     */
    @BindView(R.id.toolbar)
    protected Toolbar toolbar;

    /**
     * 内容
     */
    private FrameLayout content_base;

    /**
     * 标题title的显示
     */
    @BindView(R.id.title_tv)
    protected TextView title_tv;

    @BindView(R.id.btn_right)
    protected TextView btn_right;

    @BindView(R.id.btn_goOn)
    protected TextView btn_goOn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
    }


    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        content_base = findViewById(R.id.content_base);
        content_base.addView(LayoutInflater.from(this).inflate(layoutResID, null));
        ButterKnife.bind(this);

        toolbar.setTitle("");
        setSupportActionBar(toolbar);
    }

    /**
     * 设置Toolbar不可见
     */
    public void setToolBarInVisable() {
        toolbar.setVisibility(View.GONE);
    }


    /**
     * 设置Toolbar可见
     */
    public void setToolBarVisable() {
        toolbar.setVisibility(View.VISIBLE);
    }

    /**
     * 显示标题的显示
     *
     * @param title
     */
    protected void setTitleShow(String title) {
        title_tv.setText(title);
    }

    /**
     * 获取页面显示的标题
     *
     * @return
     */
    protected String getTitleShow() {
        return title_tv.getText().toString();
    }


    /**
     * 给toolbar设置返回按钮
     */
    protected void showLeftBack() {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    protected void setGoOnBtnListener(View.OnClickListener onClickListener) {
        setGoOnBtnListener("",onClickListener);
    }

    protected void setGoOnBtnListener(String txt,View.OnClickListener onClickListener) {
        if(!TextUtils.isEmpty(txt)){
            btn_goOn.setText(txt);
        }

        btn_goOn.setVisibility(View.VISIBLE);
        btn_goOn.setOnClickListener(onClickListener);
    }


    protected void setRightButtonEnable(String text) {
        btn_right.setText(text);
        btn_right.setVisibility(View.VISIBLE);
    }

    protected void setRightButtonListener(View.OnClickListener onClickListener) {
        btn_right.setOnClickListener(onClickListener);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * .
     * 跳转页面,并且保留当前页面
     *
     * @param cls
     */
    public void jumpActivity(Class<? extends BaseActivity> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     * 携带数据跳转页面,并且保留当前页面
     *
     * @param cls
     * @param bundle
     */
    public void jumpActivity(Class<? extends BaseActivity> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 跳转页面，关闭当前页面
     *
     * @param cls
     */
    public void skipActivity(Class<? extends BaseActivity> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        finish();
    }

    /**
     * 跳转页面，关闭当前页面
     *
     * @param cls
     */
    public void skipActivity(Class<? extends BaseActivity> cls, Bundle extras) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(extras);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
